


var alltds =document.querySelectorAll("#board2 td");
// 请求查询所有选择器-找到所有红色表格中的td
var turn="black";
// 当回合该哪一方下棋
var round=document.getElementById("round");
// 找到id为round的元素
var his=[]
// 定义一个撤销的历史
var finish=false;
// 赢了不继续
function tdClick(e)
// td的点击事件函数
{
    if(finish)
    {
        return;
    }
    var td =e.target;
    // target目标靶子
    var side=td.getAttribute("chess");
    if(side)
    {
        return;
    }
    td.setAttribute("chess",turn);
    // 回合
    // e.target.style.backgroundColor="red"确认是否有点击事件;
     winCheck(e.target.index);
    //  调用wincheck,判断这个子是否赢了，index指数，索引
    if(turn=="black")
    {
        turn="white";
        round.textContent="白方回合";
        // round的文本内容
    }
    else
    {
        turn="black";
        round.textContent="黑方回合";
    }
    his.push(e.target);
}

for(var i =0;i<alltds.length;i++)
// 创建for循环，满足前面的执行，不满足的话不执行
{
    alltds[i].onclick=tdClick;   

    alltds[i].index=i;
    // 为了编号alltds[i].textCount=i;
}
// 创建一个悔棋的函数
function back()
{
    finish=false;
    if(his.length>0)
    {
        var td =his.pop();

        td.removeAttribute("chess");
        // 清除td的chess属性
        turn=turn=="black"?"white":"black";
        // 悔棋改变颜色等于上述if-else的复制版
    }
}
// 创建一个重新开始的函数
function restart()
{
    window.location.reload();
}

function winCheck(index)
// 判断胜利的函数
{
    if(horizontalCheck(index)||verticalCheck(index)||tlbrCheck(index)||youCheck(index))
    {
        
        alert((turn=="black"?"黑":"白")+"方胜利");
        finish=true;
    }
    // if(verticalCheck(index))
    // {
    //     alert(turn+"方胜利");
    // }
    // console.log(1);j检查函数是否运行
}
// ---------横向检查函数-----------------
function horizontalCheck(index)
{
    var num=1;
 // -------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex%19!=0&&alltds[currentIndex-1].getAttribute("chess")==turn;
    // %取余 !=不等于 ==判断是否等
    // 判断左边的一个，如果是黑色的话
    while(hasNext)
    {
        num++;
        currentIndex--;
        // 往左检查另外一个
        hasNext=currentIndex%19!=0&&alltds[currentIndex-1].getAttribute("chess")==turn;
    }
    // ------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex%19!=18&&alltds[currentIndex+1].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex++;
        // 往右检查下一个
        hasNext=currentIndex%19!=18&&alltds[currentIndex+1].getAttribute("chess")==turn;
 // ----------------------------------------------------------------
        
    }
    return num>4;
}
// -----------上下竖向检查---------------
function verticalCheck(index)
{
    var num=1;
 // -------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex>=19&&alltds[currentIndex-19].getAttribute("chess")==turn;
 
    while(hasNext)
    {
        num++;
        currentIndex-=19;
        hasNext=currentIndex>=19&&alltds[currentIndex-19].getAttribute("chess")==turn;
    }
    // ------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex<342&&alltds[currentIndex+19].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex+=19;
        hasNext=currentIndex<342&&alltds[currentIndex+19].getAttribute("chess")==turn;
        
    }
    return num>4;
}
// ---------左上到右下斜线的检查----------
function tlbrCheck(index)
{
    var num=1;
    // -------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex%19!=0&&currentIndex>=19&&alltds[currentIndex-20].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex-=20;
        hasNext=currentIndex%19!=0&&currentIndex>=19&&alltds[currentIndex-20].getAttribute("chess")==turn;
    }
    // ------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex<342&&currentIndex%19!=18&&alltds[currentIndex+20].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex+=20;
        hasNext=currentIndex<342&&currentIndex%19!=18&&alltds[currentIndex+20].getAttribute("chess")==turn;
        // ----------------------------------------------------------------
        
    }
    return num>4;
}
// ------------右上到左下---------------
function youCheck(index)
{
    var num=1;
 // -------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex%19!=18&&currentIndex>=19&&alltds[currentIndex-18].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex-=18;
        hasNext=currentIndex%19!=18&&currentIndex>=19&&alltds[currentIndex-18].getAttribute("chess")==turn;
    }
    // ------------------------------------------------------------------
    var currentIndex=index;
    var hasNext=currentIndex<342&&currentIndex%19!=0&&alltds[currentIndex+18].getAttribute("chess")==turn;
    while(hasNext)
    {
        num++;
        currentIndex+=18;
        hasNext=currentIndex<342&&currentIndex%19!=0&&alltds[currentIndex+18].getAttribute("chess")==turn;
    // ----------------------------------------------------------------
        
    }
    return num>4;
}


    

